/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view

/**
 *
 * @author sneiderhc
 */
class FileReader {
    boolean read(Order woody, File file) {
        def q=["cherry":0, "maple":0, "mahogany":0,
        "wenge":0, "whiteoak":0, "sawdust":0]
        try { file.splitEachLine(": ") { line ->
            switch(line[0]) {
                case "Name": woody.setName(line[1])
                    break
                case "Address": woody.setAddress(line[1])
                    break
                case "Cherry": q["cherry"]=line[1].toInteger()
                    break
                case "Curly Maple": q["maple"]=line[1].toInteger()
                    break
                case "Genuine Mahogany": q["mahogany"]=line[1].toInteger()
                    break
                case "Wenge": q["wenge"]=line[1].toInteger()
                    break
                case "White Oak": q["whiteoak"]=line[1].toInteger()
                    break
                case "Sawdust": q["sawdust"]=line[1].toInteger()
                    break
                default: throw IOException
            }}
            if(woody.getName()=="" || woody.getAddress()=="" ||
                woody.getName()==null || woody.getAddress()==null ||
                q.values().toList()==[0, 0, 0, 0, 0, 0]) {
                throw IOException
            }
        } catch(Exception e) { return false }
        woody.setQuantity(q)
        return true
    }
}

